/*
 * Famon (http://code.google.com/p/famon/)
 * Copyright (c) 2010 Carl Crous.
 *
 * This file is part of Famon.
 *
 * Famon is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Famon is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with Famon.  If not, see <http://www.gnu.org/licenses/>.
 */

package za.org.foobar.famon.frontend.client.ui;

import com.smartgwt.client.types.ListGridEditEvent;
import com.smartgwt.client.widgets.IButton;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.ListGridField;
import com.smartgwt.client.widgets.layout.VStack;
import org.gwtportlets.portlet.client.WidgetFactory;
import org.gwtportlets.portlet.client.ui.Portlet;
import org.gwtportlets.portlet.smartgwt.client.DataTransferObject;
import org.gwtportlets.portlet.smartgwt.client.SmartPortlet;
import org.gwtportlets.portlet.smartgwt.client.SmartPortletDataSource;
import org.gwtportlets.portlet.smartgwt.client.SmartPortletFactory;
import za.org.foobar.famon.frontend.client.dto.PropertyDto;
import za.org.foobar.famon.frontend.client.util.ClientUtil;

/**
 * @author Carl Crous
 */
public class PropertyPortlet extends SmartPortlet {
    private SmartPortletDataSource dataSource;
    private ListGrid grid;
    
    public PropertyPortlet() {
        final VStack stack = new VStack(10);
        initWidget(stack);

        if (!ClientUtil.checkIsAdmin(stack)) {
            return;
        }
        
        dataSource = PropertyDto.getDataSource(this);
        dataSource.setPortlet(this);

        grid = new ListGrid();
        grid.setWidth(300);
        grid.setHeight(200);
        grid.setDataSource(dataSource);
        grid.setAutoFetchData(true);
        grid.setCanEdit(true);
        grid.setEditEvent(ListGridEditEvent.CLICK);
        grid.setModalEditing(false);

        ListGridField nameField = new ListGridField("name", "Name");
        ListGridField valueField = new ListGridField("value", "Value");

        grid.setFields(nameField, valueField);

        IButton newButton = new IButton("Edit New");
        newButton.setTop(250);
        newButton.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
                grid.startEditingNew();
            }  
        });

        IButton removeButton = new IButton("Remove");
        removeButton.setTop(250);
        removeButton.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
                grid.removeSelectedData();
            }
        });

        stack.addMember(grid);
        stack.addMember(newButton);
        stack.addMember(removeButton);

    }

    public WidgetFactory createWidgetFactory() {
        return new Factory();
    }
    
    public static class Factory extends SmartPortletFactory<PropertyPortlet> {
        public Factory() {
        }

        public Factory(Portlet p) {
            super(p);
        }

        public PropertyPortlet createWidget() {
            return new PropertyPortlet();
        }

        public DataTransferObject createDto() {
            if (PropertyDto.DATA_SOURCE_ID.equals(dataSourceId)) {
                return new PropertyDto();
            }
            return null;
        }
    }
}
